<template>
    <div class="app-container">
        <el-row :gutter="24">
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryRef" :inline="true" style="margin-left: 25px;">
                    <el-form-item label="厂商名称" prop="name">
                        <el-input v-model="queryParams.name" placeholder="请输入" clearable style="width: 220px" maxlength="30"
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="分类" prop="supplierClass">
                        <el-input v-model="queryParams.supplierClass" placeholder="请输入" clearable style="width: 220px"
                            maxlength="30" @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item style="margin-left: 0px;">
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        <el-button type="success" icon="Plus" @click="handleAdd">新增</el-button>
                    </el-form-item>
                </el-form>

                <el-table v-loading="loading" :data="detectionList"
                    style="margin-top: 30px;margin-left: 10px;margin-right: 10px;width: calc(100% - 20px);">
                    <el-table-column type="index" width="50" label="序号" />
                    <el-table-column label="分类" align="center" prop="supplierClass" />
                    <el-table-column label="厂商名称" align="center" prop="name" />
                    <el-table-column label="厂商地址" align="center" prop="address" />
                    <el-table-column label="联系人" align="center" prop="contacter" />
                    <el-table-column label="联系电话" align="center" prop="telNo" />
                    <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width"
                        width="120px">
                        <template #default="scope">
                            <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button link type="danger" @click="handleDel(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                    @pagination="getList" style="margin-right: 20px;" />
            </el-col>
        </el-row>

        <el-dialog v-model="dialogVisible" draggable :title="dialogInfo.id ? '编辑' : '新增'" width="600px">
            <el-form ref="dialogFormRef" :model="dialogInfo" label-width="100px" :rules="rules" v-loading="popLoading">
                <el-form-item label="厂商名称" prop="name" style="margin-left: 0px;">
                    <el-input v-model="dialogInfo.name" style="width: 400px" maxlength="100" />
                </el-form-item>
                <el-form-item label="厂商分类" prop="supplierClass" style="margin-left: 0px;">
                    <el-input v-model="dialogInfo.supplierClass" style="width: 400px" maxlength="100" />
                </el-form-item>
                <el-form-item label="联系人" prop="contacter" style="margin-left: 0px;">
                    <el-input v-model="dialogInfo.contacter" style="width: 400px" maxlength="20" />
                </el-form-item>
                <el-form-item label="联系电话" prop="telNo" style="margin-left: 0px;">
                    <el-input v-model="dialogInfo.telNo" style="width: 400px" maxlength="11" />
                </el-form-item>
                <el-form-item label="厂商地址" prop="address" style="margin-left: 0px;">
                    <el-input v-model="dialogInfo.address" style="width: 400px" :autosize="{ minRows: 2, maxRows: 4 }"
                        type="textarea" maxlength="200" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitEdit">确认</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
 
<script setup>
import {
    apiGetUpSupplierList,
    apiAddUpSupplierInfo,
    apiUpdateUpSupplierInfo,
    apiDelUpSupplier
} from "@/api/supplier";

import { ref } from "vue";
const { proxy } = getCurrentInstance();

const detectionList = ref([]);
const loading = ref(false);
const total = ref(0);
const dialogInfo = ref({})
const dialogVisible = ref(false)
const popLoading = ref(false)
const dialogFormRef = ref(null)
const rules = ref({
    name: [{ required: true, message: "请输入", trigger: "blur" }],
    contacter: [{ required: true, message: "请输入", trigger: "blur" }],
    telNo: [{ required: true, message: "请输入", trigger: "blur" }],
    address: [{ required: true, message: "请输入", trigger: "blur" }],
    supplierClass: [{ required: true, message: "请输入", trigger: "blur" }],
})

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
    },
});

const { queryParams } = toRefs(data);

/** 查询列表 */
const getList = () => {
    loading.value = true;
    apiGetUpSupplierList(queryParams.value).then(res => {
        loading.value = false;
        if (res.data) {
            detectionList.value = res.data.records;
            total.value = res.data.total;
        } else {
            detectionList.value = [];
            total.value = 0;
        }
    }).catch(err => {
        loading.value = false;
        detectionList.value = [];
        total.value = 0;
    });
};

getList();

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};

const handleAdd = () => {
    popLoading.value = false;
    dialogInfo.value = {}
    dialogVisible.value = true
}

const handleEdit = (row) => {
    popLoading.value = false;
    dialogInfo.value = row
    dialogVisible.value = true
};

const submitEdit = () => {
    dialogFormRef.value.validate((valid) => {
        if (valid) {
            if (dialogInfo.value.id) {
                //更新
                popLoading.value = true;
                apiUpdateUpSupplierInfo({
                    "id": dialogInfo.value.id,
                    "name": dialogInfo.value.name,
                    "address": dialogInfo.value.address,
                    "contacter": dialogInfo.value.contacter,
                    "telNo": dialogInfo.value.telNo,
                    "supplierClass": dialogInfo.value.supplierClass || '',
                }).then(res => {
                    popLoading.value = false;
                    dialogVisible.value = false
                    proxy.$modal.msgSuccess("操作成功");
                    getList()
                }).catch(err => {
                    popLoading.value = false;
                });
            } else {
                //新增
                popLoading.value = true;
                apiAddUpSupplierInfo({
                    "name": dialogInfo.value.name,
                    "address": dialogInfo.value.address,
                    "contacter": dialogInfo.value.contacter,
                    "telNo": dialogInfo.value.telNo,
                    "supplierClass": dialogInfo.value.supplierClass || '',
                }).then(res => {
                    popLoading.value = false;
                    dialogVisible.value = false
                    proxy.$modal.msgSuccess("操作成功");
                    getList()
                }).catch(err => {
                    popLoading.value = false;
                });
            }
        }
    })
};

const handleDel = (row) => {
    proxy.$modal.confirm('是否确认删除该数据项？').then(function () {
        popLoading.value = true;
        return apiDelUpSupplier(row.id)
    }).then(() => {
        proxy.$modal.msgSuccess("操作成功");
        getList();
    }).catch(() => { });
};

</script>
 

<style lang="scss" scoped>
.dialog-item-title {
    color: #000;
    font-size: 17px;
    padding-left: 10px;
    margin: 0 20px;
    border-left: 4px #00BFFF solid;
}

.now-select-txt {
    font-size: 15px;
    color: #fff;
    background: #00BFFF;
    padding: 10px 20px;
    border-radius: 10px 10px 0 0;

    span {
        font-size: 20px;
        font-weight: bold;
        margin: 0 10px;
    }

    div {
        cursor: pointer;
        margin-left: 30px;
        font-weight: bold;
    }
}
</style>